import { defineStore } from "pinia";
import { reqSpuList } from "@/api/spu";
import type { SpuListType } from "@/types/spu";

export interface SpuListState {
    spuList: SpuListType;
    total: number;
}

export const useSpuStore = defineStore("spu", {
    state: (): SpuListState => ({
        spuList: [],
        total: 0,
    }),
    actions: {
        async getSpuList(page: number, limit: number, categoryId3: number) {
            try {
                const res = await reqSpuList(page, limit, categoryId3 as number);
                this.spuList = res.records;
                this.total = res.total;
            } catch (e) {
                return Promise.reject(e);
            }
        },
        clearSpuList() {
            this.spuList = [];
            this.total = 0;
        },
    },
});
